//
//  WJRightCell.m
//  WJBSApp
//
//  Created by dllo on 16/7/14.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJRightCell.h"
#import "WJRightModel.h"
#import "WJDataManager.h"

@interface WJRightCell ()

@property (nonatomic,retain) WJBaseImageView *headeImageView;

@property (nonatomic,retain) WJBaseLable *nameLable;

@property (nonatomic,retain) WJBaseLable *fans_countLable;

@property (nonatomic,retain) UILabel *belowLable;

@end

@implementation WJRightCell

-(void)dealloc
{
    [_headeImageView release];
    [_nameLable release];
    [_fans_countLable release];
    [_gzButton release];
    [_belowLable release];
    [_model release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.headeImageView = [[WJBaseImageView alloc]init];
        [self.contentView addSubview:self.headeImageView];
        self.headeImageView.layer.cornerRadius = (HEIGHT / 9 - 20) / 2;
        self.headeImageView.layer.masksToBounds = YES;
        [_headeImageView release];

        self.nameLable = [[WJBaseLable alloc]init];
        [self.contentView addSubview:self.nameLable];
        [_nameLable release];
        
        
        self.fans_countLable = [[WJBaseLable alloc]init];
        [self.contentView addSubview:self.fans_countLable];
        [_fans_countLable release];

//#warning +关注 需要判断啊  在数据库里面  要是有就显示 取消关注
        self.gzButton = [WJBaseButton buttonWithType:UIButtonTypeSystem];
        [self.gzButton addTarget:self action:@selector(addFolow:) forControlEvents:1<<6];
        [self.contentView addSubview:self.gzButton];
        
        self.belowLable = [[UILabel alloc]init];
        self.belowLable.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.belowLable];
        [_belowLable release];
        
        
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.headeImageView.frame = CGRectMake(10, 10, HEIGHT / 9 - 20,HEIGHT / 9 - 20);
    
    self.nameLable.frame = CGRectMake(15 +  HEIGHT / 9 - 20, 10, 150, (HEIGHT / 9 - 20) / 2);
    
    self.fans_countLable.frame = CGRectMake(15 +  HEIGHT / 9 - 20, HEIGHT / 18, 150, (HEIGHT / 9- 20) / 2 - 1);
    self.gzButton.frame = CGRectMake(WIDTH * 5 / 7 - 10, (HEIGHT / 9 - 30) / 2, WIDTH / 7, 30);
    self.belowLable.frame = CGRectMake(0, HEIGHT/9 - 1, WIDTH * 6 / 7, 1);
    
}

- (void)setModel:(WJRightModel *)model
{
    if (_model !=model) {
        [_model release];
        _model = [model retain];
    }
    
    [self.headeImageView sd_setImageWithURL:[NSURL URLWithString:model.header]];
    self.nameLable.text = model.screen_name;
    if (model.is_vip) {
        self.nameLable.textColor = [UIColor redColor];
    }else{
        self.nameLable.textColor = [UIColor grayColor];
    }
    self.fans_countLable.text = [NSString stringWithFormat:@"%ld人关注",(long)model.fans_count];
    
    BOOL isExist = [[WJDataManager shareFMDB] isExistUserID:[NSString stringWithFormat:@"%ld",(long)model.uid]];
    
    if (isExist) { //也就是存在
         self.gzButton.titleLabel.font = [UIFont systemFontOfSize:11];
        [self.gzButton setTitle:@"取消关注" forState:0];
        [self.gzButton setTitleColor:[UIColor grayColor] forState:0];
        
    }else{  //也就是不存在
         self.gzButton.titleLabel.font = [UIFont systemFontOfSize:17];
        [self.gzButton setTitleColor:[UIColor redColor] forState:0];
        [self.gzButton setTitle:@"+关注" forState:0];
    }
}

- (void)addFolow:(WJEssAndNewButton *)button
{
    // ID 和Name
    NSLog(@"添加数据库啊啊啊啊啊啊啊啊啊啊啊啊");
    NSString *string = button.titleLabel.text;
    if ([string isEqualToString:@"取消关注"]) { // 执行取消关注 也就是删除
        self.gzButton.titleLabel.font = [UIFont systemFontOfSize:17];
        [button setTitle:@"+关注" forState:0];
        [button setTitleColor:[UIColor redColor] forState:0];
        [[WJDataManager shareFMDB] deleteWith:[NSString stringWithFormat:@"%ld",(long)self.model.uid]];
        
    }else{
        self.gzButton.titleLabel.font = [UIFont systemFontOfSize:11];
        [button setTitle:@"取消关注" forState:0];
        [button setTitleColor:[UIColor grayColor] forState:0];
        [[WJDataManager shareFMDB] insertWithID:[NSString stringWithFormat:@"%ld",(long)self.model.uid] username:self.model.screen_name tiezi_count:self.model.tiezi_count follow_count:[NSString stringWithFormat:@"%ld",self.model.is_follow] profile_image:self.model.header];
    }
    
}











@end
